#!/usr/bin/env python
# -*- coding: utf-8 -*-
from string import Template
from tack.lib import json_app, content_headers, status_codes
from tack_ocie.api.util import build_row_dict
from tack_ocie.api.appointment_types import get_all_sql as get_all_appointment_types
from tack_ocie.api.units import get_all_sql as get_all_units
from tack_default.api.ranks import get_rank_sql as get_all_ranks


def get(environ, start_response):
    output = {}
    sql = Template(get_all_appointment_types).substitute(locals())
    cursor = environ['connection'].cursor()
    cursor.execute(sql)
    rows = cursor.fetchall()
    throwaway = []
    for row in rows:
        row_dict = build_row_dict(cursor.description, row)
        throwaway.append(row_dict)
    output['appointment_types'] = throwaway
    sql = Template(get_all_units).substitute(locals())
    cursor = environ['connection'].cursor()
    cursor.execute(sql)
    rows = cursor.fetchall()
    throwaway = []
    for row in rows:
        row_dict = build_row_dict(cursor.description, row)
        throwaway.append(row_dict)
    output['units'] = throwaway
    sql = Template(get_all_ranks).substitute(locals())
    cursor = environ['connection'].cursor()
    cursor.execute(sql)
    rows = cursor.fetchall()
    throwaway = []
    for row in rows:
        row_dict = build_row_dict(cursor.description, row)
        throwaway.append(row_dict)
    output['ranks'] = throwaway    
    return json_app(output)(environ, start_response)
